package com.zhang.zhangapp.view.custom;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.zhang.zhangapp.R;
import com.zhang.zhangapp.utils.DensityUtils;
import com.zhang.zhangapp.utils.ScreenUtils;
import com.zhang.zhangapp.view.adapter.FilterAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/26.
 */

public class FilterPopupWindow extends PopupWindow {
    private int mWidth;
    private int mHeight;
    private View mContentView;
    private ListView listView;
    private List<Map<String, String>> filters = new ArrayList<>();
    private FilterAdapter adapter;
    private onItemClick onItemClick;
    private int y;
    private LinearLayout leftLayout;
    private Map<String, List<Map<String, String>>> filters1;
    private Context context;
    private View curSelectView;

    public FilterPopupWindow(Context context, final List<Map<String, String>> filters, int y) {
        super(context);
        this.filters = filters;
        this.y = y;
        this.context = context;
        calWidthAndHeight(context);
//        setWidth(mWidth);
        setHeight(mHeight - y);
        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new BitmapDrawable());
        mContentView = LayoutInflater.from(context).inflate(R.layout.popup_window_filter, null);
        initRightView();
    }

    public FilterPopupWindow(Context context, final Map<String, List<Map<String, String>>> filters1, int y) {
        super(context);
        this.filters = filters;
        this.y = y;
        this.context = context;
        this.filters1 = filters1;
        calWidthAndHeight(context);
//        setWidth(mWidth);
        setHeight(mHeight - y);
        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new BitmapDrawable());
        mContentView = LayoutInflater.from(context).inflate(R.layout.popup_window_filter, null);
        leftLayout = mContentView.findViewById(R.id.left_layout);
        initRightView();
        initLeftView();
    }

    private void initLeftView() {
        if (filters1 != null) {
            int margin = DensityUtils.dp2px(context, 12);
            int paddingTop = DensityUtils.dp2px(context, 6);
            int width = mWidth/4;
            int i = 0;
            for (String key : filters1.keySet()) {
                TextView textView = new TextView(context);
                textView.setBackgroundResource(R.drawable.recharge_text_bg);
                textView.setTextColor(context.getResources().getColorStateList(R.color.recharge_text_color));
                textView.setText(key);
                textView.setPadding(margin, paddingTop*4/5, margin, paddingTop*4/5);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.WRAP_CONTENT);
                textView.setGravity(Gravity.CENTER);
                leftLayout.addView(textView);
                textView.setTag(filters1.get(key));
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(!view.isSelected()){
                            selectLeftView(view);
                        }
                    }
                });
                if (i == 0) {
                    selectLeftView(textView);
                    layoutParams.setMargins(margin, paddingTop, margin, paddingTop/2);
                }else{
                    layoutParams.setMargins(margin, paddingTop/2, margin, paddingTop/2);
                }
                textView.setLayoutParams(layoutParams);
                i++;
            }
        }
    }

    private void selectLeftView(View view) {
        if (curSelectView != null)
            curSelectView.setSelected(false);
        view.setSelected(true);
        List<Map<String, String>> filter = (List<Map<String, String>>) view.getTag();
        filters.clear();
        filters.addAll(filter);
        adapter.notifyDataSetChanged();
        curSelectView = view;
    }

    private void initRightView() {
        //设置布局与相关属性
        listView = mContentView.findViewById(R.id.list_view);
        adapter = new FilterAdapter(filters, context);
        listView.setAdapter(adapter);

        setContentView(mContentView);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                for (Map<String, String> map : filters) {
                    map.put("selected", "false");
                }
                filters.get(i).put("selected", "true");
                adapter.notifyDataSetChanged();
                if (onItemClick != null)
                    onItemClick.onItemClick(filters.get(i));
            }
        });
        setFocusable(true);
        setTouchable(true);
        setTouchable(true);
        setAnimationStyle(R.style.popup_wind_style);
        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //点击PopupWindow以外区域时PopupWindow消失
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    dismiss();
                }
                return false;
            }
        });
    }

    public FilterPopupWindow(View contentView, int width, int height) {
        super(contentView, width, height);
    }

    public void setonItemClick(onItemClick listener) {
        this.onItemClick = listener;
    }

    private void calWidthAndHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);

        mWidth = metrics.widthPixels;
        //设置高度为全屏高度的70%
        mHeight = (int) (metrics.heightPixels);
    }


    public interface onItemClick {
        void onItemClick(Map<String, String> map);
    }
}
